package com.zqxq.lottery.controller.user;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.entity.po.user.UserWechatPO;
import com.zqxq.lottery.service.user.IUserWechatService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("/wechat")
public class UserWechatController extends UserBaseController{

    @Autowired
    private IUserWechatService userWechatService;

    @ApiOperation("绑定微信")
    @PostMapping("/bindWechat")
    public Result bindAlipay(Long userId, String unionId, String openId){
        Result result = new Result<>();
        if(userId == null || StrUtils.isEmpty(unionId) || StrUtils.isEmpty(openId)){
            return new Result<>(CodeEnum.FAIL_100002.getCode(), CodeEnum.FAIL_100002.getMsg());
        }
        try {
            userWechatService.bindWechat(userId,unionId,openId);
        } catch (Exception e) {
            result.setCode(CodeEnum.FAIL_100005.getCode());
            result.setMsg(CodeEnum.FAIL_100005.getMsg());
        }
        return result;
    }

    @ApiOperation("获取用户微信绑定关系")
    @PostMapping("/queryUserWechat")
    public Result<List<UserWechatPO>> queryUserWechat(String openId){
        Result result = new Result<>();
        if(StrUtils.isEmpty(openId)){
            return new Result<>(CodeEnum.FAIL_100002.getCode(), CodeEnum.FAIL_100002.getMsg());
        }
        try {
            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("open_id", openId);
            List<UserWechatPO> list= userWechatService.selectList(wrapper);
            result.setData(list);
        } catch (Exception e) {
            result.setCode(CodeEnum.FAIL_100005.getCode());
            result.setMsg(CodeEnum.FAIL_100005.getMsg());
        }
        return result;
    }

}
